#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "Functions.h"
Uint32 obtenirPixel(SDL_Surface *surface, int x, int y)
{
    /*nbOctetsParPixel représente le nombre d'octets utilisés pour stocker un pixel.
    En multipliant ce nombre d'octets par 8 (un octet = 8 bits), on obtient la profondeur de couleur
    de l'image : 8, 16, 24 ou 32 bits.*/
    int nbOctetsParPixel = surface->format->BytesPerPixel;
    /* Ici p est l'adresse du pixel que l'on veut connaitre */
    /*surface->pixels contient l'adresse du premier pixel de l'image*/
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * nbOctetsParPixel;

    /*Gestion différente suivant le nombre d'octets par pixel de l'image*/
    switch(nbOctetsParPixel)
    {
    case 1:
        return *p;

    case 2:
        return *(Uint16 *)p;

    case 3:
        /*Suivant l'architecture de la machine*/
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
            return p[0] << 16 | p[1] << 8 | p[2];
        else
            return p[0] | p[1] << 8 | p[2] << 16;

    case 4:
        return *(Uint32 *)p;

        /*Ne devrait pas arriver, mais évite les erreurs*/
    default:
        return 0;
    }
}

void SetPosition(SDL_Rect *Thing,int x,int y,int w,int h)
{
    Thing->x=x;
    Thing->y=y;
    if (w!=0 && h!=0)
    {
        Thing->w=w;
        Thing->h=h;

    }

}
void LoadPerso(perso *MyPerso,int stage)
{
    if (stage==1){
    MyPerso->pic[0]=IMG_Load("0a.gif");
    MyPerso->pic[1]=IMG_Load("1a.gif");
    MyPerso->pic[2]=IMG_Load("2a.gif");
    MyPerso->pic[3]=IMG_Load("3a.gif");
    MyPerso->pic[4]=IMG_Load("4a.gif");
    }
}
void chargebdv(s_bdv *t_bdv)
{
    t_bdv->bdv[0]=IMG_Load("bdv0.gif");
    t_bdv->bdv[1]=IMG_Load("bdv1.gif");
    t_bdv->bdv[2]=IMG_Load("bdv2.gif");
    t_bdv->bdv[3]=IMG_Load("bdv3.gif");
    t_bdv->bdv[4]=IMG_Load("bdv4.gif");
    t_bdv->bdv[5]=IMG_Load("bdv5.gif");
    t_bdv->bdv[6]=IMG_Load("bdv6.gif");

}
void aafsa(SDL_Surface *fond,SDL_Surface *screen,SDL_Surface *PersoPic,SDL_Surface *surbdv,SDL_Surface *obspic,SDL_Rect cam,SDL_Rect PersoPos,SDL_Rect obs,SDL_Rect bdv)
{
    int n=0;
    while (n<10)
    {
        cam.x+=10;
        PersoPos.x-=10;
        PersoPos.y+=10;
        obs.y+=20;
        bdv.y+=15;
        SDL_BlitSurface(fond,&cam,screen,NULL);
        SDL_BlitSurface(surbdv,&bdv,screen,NULL);
        SDL_BlitSurface(obspic,&obs,screen,NULL);
        SDL_BlitSurface(PersoPic,NULL,screen,&PersoPos);
        SDL_Flip(screen);
        SDL_Delay(50);
        cam.x-=10;
        PersoPos.x+=10;
        PersoPos.y-=10;
        obs.y-=20;
        bdv.y-=15;
        SDL_BlitSurface(fond,&cam,screen,NULL);
        SDL_BlitSurface(surbdv,&bdv,screen,NULL);
        SDL_BlitSurface(obspic,&obs,screen,NULL);
        SDL_BlitSurface(PersoPic,NULL,screen,&PersoPos);
        SDL_Flip(screen);
        SDL_Delay(50);
        n+=1;


    }


}




int play_Stage_one(SDL_Surface *screen,perso kheop)
{

    s_bdv t_bdv;
    SDL_Surface *fond,*obs,*noob,*fondb;
    SDL_Event event;
    SDL_Rect obsset,noobs,cam,col;
    SDL_PixelFormat *fmt;
    int i=100000;
    int points =0;
    int obs_move=15,gravity=0;
    int MV = 20;
    int MH = 7;
    int keys[322]= {0};
    int quittomenu=0;
    int goul=0;
    const int FPS = 50;
    Uint32 start;
    SetPosition(&cam,0,0,0,0);
    SetPosition(&noobs,900,80,0,0);
    SetPosition(&obsset,180,550,0,0);
    SetPosition(&kheop.set,0,0,0,0);
    SetPosition(&t_bdv.pos_bdv,0,0,0,0);
    SetPosition(&col,0,0,0,0);
    SDL_Init(SDL_INIT_EVERYTHING);
    screen = SDL_SetVideoMode( 1300, 714, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    fondb=IMG_Load("background-ice-gamebb.gif");
    noob=IMG_Load("NoobZoneLogo.jpg");
    obs=IMG_Load("pic.gif");
    fond=IMG_Load("background-ice-game.gif");
    chargebdv(&t_bdv);
    Uint32 colorkey = SDL_MapRGB( kheop.pic[0]->format, 255, 255, 255 );
    Uint32 colorkey01 = SDL_MapRGB( kheop.pic[1]->format, 255, 255, 255 );
    Uint32 colorkey02 = SDL_MapRGB( kheop.pic[2]->format, 255, 255, 255 );
    Uint32 colorkey03 = SDL_MapRGB( kheop.pic[3]->format, 255, 255, 255 );
    Uint32 colorkey04 = SDL_MapRGB( kheop.pic[4]->format, 255, 255, 255 );
    Uint32 colorkey1 = SDL_MapRGB( fondb->format, 255, 255, 255 );
    Uint32 colorkeyb = SDL_MapRGB( fondb->format, 0, 0, 0 );
    t_bdv.curent = t_bdv.bdv[points];
    SDL_BlitSurface(fond,&cam,screen,NULL);
    SDL_BlitSurface(t_bdv.curent,NULL,screen,NULL);
    SDL_BlitSurface(kheop.pic[0],NULL,screen,&kheop.set);
    SDL_EnableKeyRepeat(70,35);

    while(quittomenu == 0)
    {
        obsset.y+=obs_move;
        if(obsset.y<=25|| obsset.y>600)
        {
            obs_move=(-1)*obs_move;
        }
        start = SDL_GetTicks();
        gravity=1;

        while (SDL_PollEvent(&event))
        {
            if( event.type == SDL_QUIT )
            {
                quittomenu = 1;
            }
            if(event.type==SDL_KEYDOWN)
            {
                keys[event.key.keysym.sym]=1;

            }
            if(event.type==SDL_KEYUP)
            {
                keys[event.key.keysym.sym]=0;
            }

        }

        if( keys[SDLK_RIGHT] )
        {
            kheop.set.x+=MH;
            col.x+=MH;
            i++;
            if(kheop.set.x>500&&col.x<4000)
            {
                cam.x+=MH;
                kheop.set.x-=MH;
                obsset.x-=MH;
            }
        }
        if( keys[SDLK_LEFT] )
        {
            kheop.set.x-=MH;
            col.x-=MH;
            i--;
        }

        if( keys[SDLK_UP])
        {
            if (goul==0)
            {
                kheop.set.y-=MV;
            }
        }

        kheop.curent = kheop.pic[i%5];


        if(obsset.x>kheop.set.x && obsset.x  + obs->w < kheop.set.x + kheop.curent->w && obsset.y < kheop.set.y && obsset.y + obs->h > kheop.set.y)
        {
            SDL_BlitSurface(noob,NULL,screen,&noobs);
            kheop.set.x-=50;
		col.x-=50;
            points += 1;
            if(points==6)
            {
                break;
            }
            t_bdv.curent = t_bdv.bdv[points];
            aafsa(fond,screen,kheop.curent,t_bdv.curent,obs,cam,kheop.set,obsset,t_bdv.pos_bdv);
          //  SDL_BlitSurface(t_bdv.curent,NULL,screen,NULL);
        }


        if (obtenirPixel(fondb,(col.x+(kheop.curent->w)),(kheop.set.y)+(kheop.curent->h))==colorkeyb || obtenirPixel(fondb,(col.x+((kheop.curent->w)/2)),(kheop.set.y))==colorkey1)
        {
            goul=1;
        }
        if (obtenirPixel(fondb,(col.x+((kheop.curent->w)/2)),(kheop.set.y+(kheop.curent->h)))==colorkey1)
        {
            goul=0;
        }


        if (obtenirPixel(fondb,(col.x+((kheop.curent->w)/2)),(kheop.set.y+(kheop.curent->h)))!=colorkey1)
        {
            kheop.set.y+=1;
            gravity=0;
        }

        if (obtenirPixel(fondb,(col.x+(kheop.curent->w)),(kheop.set.y+(kheop.curent->h))==colorkey1 && obtenirPixel(fondb,(col.x+((kheop.curent->w)/2)),(kheop.set.y+(kheop.curent->h)))!=colorkey1))
        {
            kheop.set.y-=3;
        }


        if(col.x>4780)quittomenu=1;
//////////////////////////////////////////////////////////////////////////

        if(1000/FPS > SDL_GetTicks()-start)
        {
            SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
        }

        if (gravity==0)kheop.set.y+=10;

        SDL_BlitSurface(fond,&cam,screen,NULL);
        SDL_BlitSurface(kheop.curent,NULL,screen,&kheop.set);
        SDL_BlitSurface(t_bdv.curent,NULL,screen,NULL);
        if(obsset.x>1)SDL_BlitSurface(obs,NULL,screen,&obsset);
        SDL_Flip(screen);

    }
    SDL_FreeSurface(kheop.pic[0]);
    SDL_FreeSurface(kheop.pic[1]);
    SDL_FreeSurface(kheop.pic[2]);
    SDL_FreeSurface(kheop.pic[3]);
    SDL_FreeSurface(kheop.pic[4]);
    SDL_FreeSurface(kheop.curent);
    SDL_FreeSurface(fond);
    SDL_FreeSurface(screen);
    SDL_FreeSurface(noob);
    SDL_FreeSurface(obs);
    SDL_FreeSurface(fondb);
    return 0;
}